package com.zch.jh.web.rest;

import com.zch.jh.JhbbApp;
import com.zch.jh.domain.Logistic;
import com.zch.jh.repository.LogisticRepository;
import com.zch.jh.service.LogisticService;
import com.zch.jh.service.dto.LogisticDTO;
import com.zch.jh.service.mapper.LogisticMapper;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.transaction.annotation.Transactional;
import javax.persistence.EntityManager;
import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;
import static org.hamcrest.Matchers.hasItem;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;

/**
 * Integration tests for the {@link LogisticResource} REST controller.
 */
@SpringBootTest(classes = JhbbApp.class)
@AutoConfigureMockMvc
@WithMockUser
public class LogisticResourceIT {

    private static final Long DEFAULT_ORDER_ID = 1L;
    private static final Long UPDATED_ORDER_ID = 2L;

    private static final String DEFAULT_SEND_NAME = "AAAAAAAAAA";
    private static final String UPDATED_SEND_NAME = "BBBBBBBBBB";

    private static final String DEFAULT_SEND_PHONE = "AAAAAAAAAA";
    private static final String UPDATED_SEND_PHONE = "BBBBBBBBBB";

    private static final String DEFAULT_SEND_ADDRESS = "AAAAAAAAAA";
    private static final String UPDATED_SEND_ADDRESS = "BBBBBBBBBB";

    private static final String DEFAULT_RECEIVE_NAME = "AAAAAAAAAA";
    private static final String UPDATED_RECEIVE_NAME = "BBBBBBBBBB";

    private static final String DEFAULT_RECEIVE_PHONE = "AAAAAAAAAA";
    private static final String UPDATED_RECEIVE_PHONE = "BBBBBBBBBB";

    private static final String DEFAULT_RECEIVE_ADDRESS = "AAAAAAAAAA";
    private static final String UPDATED_RECEIVE_ADDRESS = "BBBBBBBBBB";

    private static final String DEFAULT_LOGISTIC_NO = "AAAAAAAAAA";
    private static final String UPDATED_LOGISTIC_NO = "BBBBBBBBBB";

    private static final String DEFAULT_LOGISTIC_COMPANY = "AAAAAAAAAA";
    private static final String UPDATED_LOGISTIC_COMPANY = "BBBBBBBBBB";

    private static final String DEFAULT_SEND_TIME = "AAAAAAAAAA";
    private static final String UPDATED_SEND_TIME = "BBBBBBBBBB";

    private static final String DEFAULT_RECEIVED_TIME = "AAAAAAAAAA";
    private static final String UPDATED_RECEIVED_TIME = "BBBBBBBBBB";

    @Autowired
    private LogisticRepository logisticRepository;

    @Autowired
    private LogisticMapper logisticMapper;

    @Autowired
    private LogisticService logisticService;

    @Autowired
    private EntityManager em;

    @Autowired
    private MockMvc restLogisticMockMvc;

    private Logistic logistic;

    /**
     * Create an entity for this test.
     *
     * This is a static method, as tests for other entities might also need it,
     * if they test an entity which requires the current entity.
     */
    public static Logistic createEntity(EntityManager em) {
        Logistic logistic = new Logistic()
            .orderId(DEFAULT_ORDER_ID)
            .sendName(DEFAULT_SEND_NAME)
            .sendPhone(DEFAULT_SEND_PHONE)
            .sendAddress(DEFAULT_SEND_ADDRESS)
            .receiveName(DEFAULT_RECEIVE_NAME)
            .receivePhone(DEFAULT_RECEIVE_PHONE)
            .receiveAddress(DEFAULT_RECEIVE_ADDRESS)
            .logisticNo(DEFAULT_LOGISTIC_NO)
            .logisticCompany(DEFAULT_LOGISTIC_COMPANY)
            .sendTime(DEFAULT_SEND_TIME)
            .receivedTime(DEFAULT_RECEIVED_TIME);
        return logistic;
    }
    /**
     * Create an updated entity for this test.
     *
     * This is a static method, as tests for other entities might also need it,
     * if they test an entity which requires the current entity.
     */
    public static Logistic createUpdatedEntity(EntityManager em) {
        Logistic logistic = new Logistic()
            .orderId(UPDATED_ORDER_ID)
            .sendName(UPDATED_SEND_NAME)
            .sendPhone(UPDATED_SEND_PHONE)
            .sendAddress(UPDATED_SEND_ADDRESS)
            .receiveName(UPDATED_RECEIVE_NAME)
            .receivePhone(UPDATED_RECEIVE_PHONE)
            .receiveAddress(UPDATED_RECEIVE_ADDRESS)
            .logisticNo(UPDATED_LOGISTIC_NO)
            .logisticCompany(UPDATED_LOGISTIC_COMPANY)
            .sendTime(UPDATED_SEND_TIME)
            .receivedTime(UPDATED_RECEIVED_TIME);
        return logistic;
    }

    @BeforeEach
    public void initTest() {
        logistic = createEntity(em);
    }

    @Test
    @Transactional
    public void createLogistic() throws Exception {
        int databaseSizeBeforeCreate = logisticRepository.findAll().size();
        // Create the Logistic
        LogisticDTO logisticDTO = logisticMapper.toDto(logistic);
        restLogisticMockMvc.perform(post("/api/logistics")
            .contentType(MediaType.APPLICATION_JSON)
            .content(TestUtil.convertObjectToJsonBytes(logisticDTO)))
            .andExpect(status().isCreated());

        // Validate the Logistic in the database
        List<Logistic> logisticList = logisticRepository.findAll();
        assertThat(logisticList).hasSize(databaseSizeBeforeCreate + 1);
        Logistic testLogistic = logisticList.get(logisticList.size() - 1);
        assertThat(testLogistic.getOrderId()).isEqualTo(DEFAULT_ORDER_ID);
        assertThat(testLogistic.getSendName()).isEqualTo(DEFAULT_SEND_NAME);
        assertThat(testLogistic.getSendPhone()).isEqualTo(DEFAULT_SEND_PHONE);
        assertThat(testLogistic.getSendAddress()).isEqualTo(DEFAULT_SEND_ADDRESS);
        assertThat(testLogistic.getReceiveName()).isEqualTo(DEFAULT_RECEIVE_NAME);
        assertThat(testLogistic.getReceivePhone()).isEqualTo(DEFAULT_RECEIVE_PHONE);
        assertThat(testLogistic.getReceiveAddress()).isEqualTo(DEFAULT_RECEIVE_ADDRESS);
        assertThat(testLogistic.getLogisticNo()).isEqualTo(DEFAULT_LOGISTIC_NO);
        assertThat(testLogistic.getLogisticCompany()).isEqualTo(DEFAULT_LOGISTIC_COMPANY);
        assertThat(testLogistic.getSendTime()).isEqualTo(DEFAULT_SEND_TIME);
        assertThat(testLogistic.getReceivedTime()).isEqualTo(DEFAULT_RECEIVED_TIME);
    }

    @Test
    @Transactional
    public void createLogisticWithExistingId() throws Exception {
        int databaseSizeBeforeCreate = logisticRepository.findAll().size();

        // Create the Logistic with an existing ID
        logistic.setId(1L);
        LogisticDTO logisticDTO = logisticMapper.toDto(logistic);

        // An entity with an existing ID cannot be created, so this API call must fail
        restLogisticMockMvc.perform(post("/api/logistics")
            .contentType(MediaType.APPLICATION_JSON)
            .content(TestUtil.convertObjectToJsonBytes(logisticDTO)))
            .andExpect(status().isBadRequest());

        // Validate the Logistic in the database
        List<Logistic> logisticList = logisticRepository.findAll();
        assertThat(logisticList).hasSize(databaseSizeBeforeCreate);
    }


    @Test
    @Transactional
    public void getAllLogistics() throws Exception {
        // Initialize the database
        logisticRepository.saveAndFlush(logistic);

        // Get all the logisticList
        restLogisticMockMvc.perform(get("/api/logistics?sort=id,desc"))
            .andExpect(status().isOk())
            .andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE))
            .andExpect(jsonPath("$.[*].id").value(hasItem(logistic.getId().intValue())))
            .andExpect(jsonPath("$.[*].orderId").value(hasItem(DEFAULT_ORDER_ID.intValue())))
            .andExpect(jsonPath("$.[*].sendName").value(hasItem(DEFAULT_SEND_NAME)))
            .andExpect(jsonPath("$.[*].sendPhone").value(hasItem(DEFAULT_SEND_PHONE)))
            .andExpect(jsonPath("$.[*].sendAddress").value(hasItem(DEFAULT_SEND_ADDRESS)))
            .andExpect(jsonPath("$.[*].receiveName").value(hasItem(DEFAULT_RECEIVE_NAME)))
            .andExpect(jsonPath("$.[*].receivePhone").value(hasItem(DEFAULT_RECEIVE_PHONE)))
            .andExpect(jsonPath("$.[*].receiveAddress").value(hasItem(DEFAULT_RECEIVE_ADDRESS)))
            .andExpect(jsonPath("$.[*].logisticNo").value(hasItem(DEFAULT_LOGISTIC_NO)))
            .andExpect(jsonPath("$.[*].logisticCompany").value(hasItem(DEFAULT_LOGISTIC_COMPANY)))
            .andExpect(jsonPath("$.[*].sendTime").value(hasItem(DEFAULT_SEND_TIME)))
            .andExpect(jsonPath("$.[*].receivedTime").value(hasItem(DEFAULT_RECEIVED_TIME)));
    }
    
    @Test
    @Transactional
    public void getLogistic() throws Exception {
        // Initialize the database
        logisticRepository.saveAndFlush(logistic);

        // Get the logistic
        restLogisticMockMvc.perform(get("/api/logistics/{id}", logistic.getId()))
            .andExpect(status().isOk())
            .andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE))
            .andExpect(jsonPath("$.id").value(logistic.getId().intValue()))
            .andExpect(jsonPath("$.orderId").value(DEFAULT_ORDER_ID.intValue()))
            .andExpect(jsonPath("$.sendName").value(DEFAULT_SEND_NAME))
            .andExpect(jsonPath("$.sendPhone").value(DEFAULT_SEND_PHONE))
            .andExpect(jsonPath("$.sendAddress").value(DEFAULT_SEND_ADDRESS))
            .andExpect(jsonPath("$.receiveName").value(DEFAULT_RECEIVE_NAME))
            .andExpect(jsonPath("$.receivePhone").value(DEFAULT_RECEIVE_PHONE))
            .andExpect(jsonPath("$.receiveAddress").value(DEFAULT_RECEIVE_ADDRESS))
            .andExpect(jsonPath("$.logisticNo").value(DEFAULT_LOGISTIC_NO))
            .andExpect(jsonPath("$.logisticCompany").value(DEFAULT_LOGISTIC_COMPANY))
            .andExpect(jsonPath("$.sendTime").value(DEFAULT_SEND_TIME))
            .andExpect(jsonPath("$.receivedTime").value(DEFAULT_RECEIVED_TIME));
    }
    @Test
    @Transactional
    public void getNonExistingLogistic() throws Exception {
        // Get the logistic
        restLogisticMockMvc.perform(get("/api/logistics/{id}", Long.MAX_VALUE))
            .andExpect(status().isNotFound());
    }

    @Test
    @Transactional
    public void updateLogistic() throws Exception {
        // Initialize the database
        logisticRepository.saveAndFlush(logistic);

        int databaseSizeBeforeUpdate = logisticRepository.findAll().size();

        // Update the logistic
        Logistic updatedLogistic = logisticRepository.findById(logistic.getId()).get();
        // Disconnect from session so that the updates on updatedLogistic are not directly saved in db
        em.detach(updatedLogistic);
        updatedLogistic
            .orderId(UPDATED_ORDER_ID)
            .sendName(UPDATED_SEND_NAME)
            .sendPhone(UPDATED_SEND_PHONE)
            .sendAddress(UPDATED_SEND_ADDRESS)
            .receiveName(UPDATED_RECEIVE_NAME)
            .receivePhone(UPDATED_RECEIVE_PHONE)
            .receiveAddress(UPDATED_RECEIVE_ADDRESS)
            .logisticNo(UPDATED_LOGISTIC_NO)
            .logisticCompany(UPDATED_LOGISTIC_COMPANY)
            .sendTime(UPDATED_SEND_TIME)
            .receivedTime(UPDATED_RECEIVED_TIME);
        LogisticDTO logisticDTO = logisticMapper.toDto(updatedLogistic);

        restLogisticMockMvc.perform(put("/api/logistics")
            .contentType(MediaType.APPLICATION_JSON)
            .content(TestUtil.convertObjectToJsonBytes(logisticDTO)))
            .andExpect(status().isOk());

        // Validate the Logistic in the database
        List<Logistic> logisticList = logisticRepository.findAll();
        assertThat(logisticList).hasSize(databaseSizeBeforeUpdate);
        Logistic testLogistic = logisticList.get(logisticList.size() - 1);
        assertThat(testLogistic.getOrderId()).isEqualTo(UPDATED_ORDER_ID);
        assertThat(testLogistic.getSendName()).isEqualTo(UPDATED_SEND_NAME);
        assertThat(testLogistic.getSendPhone()).isEqualTo(UPDATED_SEND_PHONE);
        assertThat(testLogistic.getSendAddress()).isEqualTo(UPDATED_SEND_ADDRESS);
        assertThat(testLogistic.getReceiveName()).isEqualTo(UPDATED_RECEIVE_NAME);
        assertThat(testLogistic.getReceivePhone()).isEqualTo(UPDATED_RECEIVE_PHONE);
        assertThat(testLogistic.getReceiveAddress()).isEqualTo(UPDATED_RECEIVE_ADDRESS);
        assertThat(testLogistic.getLogisticNo()).isEqualTo(UPDATED_LOGISTIC_NO);
        assertThat(testLogistic.getLogisticCompany()).isEqualTo(UPDATED_LOGISTIC_COMPANY);
        assertThat(testLogistic.getSendTime()).isEqualTo(UPDATED_SEND_TIME);
        assertThat(testLogistic.getReceivedTime()).isEqualTo(UPDATED_RECEIVED_TIME);
    }

    @Test
    @Transactional
    public void updateNonExistingLogistic() throws Exception {
        int databaseSizeBeforeUpdate = logisticRepository.findAll().size();

        // Create the Logistic
        LogisticDTO logisticDTO = logisticMapper.toDto(logistic);

        // If the entity doesn't have an ID, it will throw BadRequestAlertException
        restLogisticMockMvc.perform(put("/api/logistics")
            .contentType(MediaType.APPLICATION_JSON)
            .content(TestUtil.convertObjectToJsonBytes(logisticDTO)))
            .andExpect(status().isBadRequest());

        // Validate the Logistic in the database
        List<Logistic> logisticList = logisticRepository.findAll();
        assertThat(logisticList).hasSize(databaseSizeBeforeUpdate);
    }

    @Test
    @Transactional
    public void deleteLogistic() throws Exception {
        // Initialize the database
        logisticRepository.saveAndFlush(logistic);

        int databaseSizeBeforeDelete = logisticRepository.findAll().size();

        // Delete the logistic
        restLogisticMockMvc.perform(delete("/api/logistics/{id}", logistic.getId())
            .accept(MediaType.APPLICATION_JSON))
            .andExpect(status().isNoContent());

        // Validate the database contains one less item
        List<Logistic> logisticList = logisticRepository.findAll();
        assertThat(logisticList).hasSize(databaseSizeBeforeDelete - 1);
    }
}
